<ion-header>
    <ion-navbar core-back-button>
        <ion-title><core-format-text [text]="title" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-format-text></ion-title>
        <ion-buttons end [hidden]="!loaded">
            <button *ngIf="assessmentId && access.assessingallowed" ion-button clear (click)="saveAssessment()" [attr.aria-label]="'core.save' | translate">
                {{ 'core.save' | translate }}
            </button>
            <button *ngIf="canAddFeedback" ion-button clear (click)="saveEvaluation()" [attr.aria-label]="'core.save' | translate">
                {{ 'core.save' | translate }}
            </button>
        </ion-buttons>
    </ion-navbar>
</ion-header>
<ion-content>
    <ion-refresher [enabled]="loaded" (ionRefresh)="refreshSubmission($event)" *ngIf="!((assessmentId && access.assessingallowed) || canAddFeedback)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>
    <core-loading [hideUntil]="loaded">
        <ion-list *ngIf="submission">
            <addon-mod-workshop-submission [submission]="submission" [courseId]="courseId" [module]="module" [workshop]="workshop" [access]="access"></addon-mod-workshop-submission>
            <ion-item text-wrap *ngIf="canEdit || canDelete">
                <button ion-button block icon-start *ngIf="canEdit" (click)="editSubmission()">
                    <ion-icon name="create"></ion-icon>
                    {{ 'addon.mod_workshop.editsubmission' | translate }}
                </button>
                <button ion-button block icon-start *ngIf="!submission.deleted && canDelete" color="danger" (click)="deleteSubmission()">
                    <ion-icon name="trash"></ion-icon>
                    {{ 'addon.mod_workshop.deletesubmission' | translate }}
                </button>
                <button ion-button block icon-start outline *ngIf="submission.deleted && canDelete" color="danger" (click)="undoDeleteSubmission()">
                    <ion-icon name="undo"></ion-icon>
                    {{ 'core.restore' | translate }}
                </button>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="!canAddFeedback && evaluate && evaluate.text">
            <ion-item text-wrap>
                <ion-avatar core-user-avatar *ngIf="evaluateByProfile" [user]="evaluateByProfile" item-start [courseId]="courseId" [userId]="evaluateByProfile.id"></ion-avatar>
                <h2 *ngIf="evaluateByProfile && evaluateByProfile.fullname">{{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateByProfile.fullname} }}</h2>
                <core-format-text [text]="evaluate.text" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-format-text>
            </ion-item>
        </ion-list>

        <ion-list *ngIf="ownAssessment && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.yourassessment' | translate }}</h2>
            </ion-item>
            <addon-mod-workshop-assessment [submission]="submission" [assessment]="ownAssessment" [courseId]="courseId" summary="true" [access]="access" [module]="module" [workshop]="workshop"></addon-mod-workshop-assessment>
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewedby && submissionInfo.reviewedby.length && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.receivedgrades' | translate }}</h2>
            </ion-item>
            <ng-container *ngFor="let reviewer of submissionInfo.reviewedby">
                <addon-mod-workshop-assessment *ngIf="!reviewer.ownAssessment" [submission]="submission" [assessment]="reviewer" [courseId]="courseId" summary="true" [access]="access" [workshop]="workshop"></addon-mod-workshop-assessment>
            </ng-container>
        </ion-list>

        <ion-list *ngIf="submissionInfo && submissionInfo.reviewerof && submissionInfo.reviewerof.length && !assessment">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.givengrades' | translate }}</h2>
            </ion-item>
            <addon-mod-workshop-assessment *ngFor="let reviewer of submissionInfo.reviewerof" [assessment]="reviewer" [courseId]="courseId" summary="true" [workshop]="workshop" [access]="access"></addon-mod-workshop-assessment>
        </ion-list>

        <form ion-list [formGroup]="feedbackForm" *ngIf="canAddFeedback">
            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</h2>
            </ion-item>
            <ion-item text-wrap *ngIf="access.canpublishsubmissions">
                <ion-label>{{ 'addon.mod_workshop.publishsubmission' | translate }}</ion-label>
                <ion-toggle formControlName="published"></ion-toggle>
                <p class="item-help">{{ 'addon.mod_workshop.publishsubmission_help' | translate }}</p>
            </ion-item>

            <ion-item text-wrap>
                <h2>{{ 'addon.mod_workshop.gradecalculated' | translate }}</h2>
                <p>{{ submission.submissiongrade }}</p>
            </ion-item>
            <ion-item text-wrap>
                <ion-label stacked>{{ 'addon.mod_workshop.gradeover' | translate }}</ion-label>
                <ion-select formControlName="grade" interface="action-sheet">
                    <ion-option *ngFor="let grade of evaluationGrades" [value]="grade.value">{{grade.label}}</ion-option>
                </ion-select>
            </ion-item>
            <ion-item>
                <ion-label stacked>{{ 'addon.mod_workshop.feedbackauthor' | translate }}</ion-label>
                <core-rich-text-editor item-content [control]="feedbackForm.controls['text']" formControlName="text"></core-rich-text-editor>
            </ion-item>
        </form>

        <addon-mod-workshop-assessment-strategy *ngIf="assessmentId" [workshop]="workshop" [access]="access" [assessmentId]="assessmentId" [userId]="assessmentUserId" [strategy]="strategy" [edit]="access.assessingallowed"></addon-mod-workshop-assessment-strategy>

        <ion-list *ngIf="assessmentId && !access.assessingallowed && assessment.feedbackreviewer">
            <ion-item text-wrap>
                <ion-avatar core-user-avatar *ngIf="evaluateGradingByProfile" [user]="evaluateGradingByProfile" item-start [courseId]="courseId" [userId]="evaluateGradingByProfile.id"></ion-avatar>
                <h2 *ngIf="evaluateGradingByProfile && evaluateGradingByProfile.fullname">{{ 'addon.mod_workshop.feedbackby' | translate : {$a: evaluateGradingByProfile.fullname} }}</h2>
                <core-format-text [text]="assessment.feedbackreviewer" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-format-text>
            </ion-item>
        </ion-list>
    </core-loading>
</ion-content>
